﻿function GameMessageBuffer(gameName, gameNumber, assignmentId) {

    this.gameName = gameName;
    this.gameNumber = gameNumber;
    this.assignmentId = assignmentId;

    this.messagesList = [];
    this.messageSendingTimeInterval = 10 * 1000;
    this.lastMessageSendingTime = 0;

    this.sendMessagesRoutine = function(time) {
        if (this.lastMessageSendingTime < time) {
            this.lastMessageSendingTime = time + this.messageSendingTimeInterval;
            this.sendMessages();
        }
    }

    this.popMessageList = function() {
        var list = {
            gameName: this.gameName,
            gameNumber: this.gameNumber,
            assignmentId: this.assignmentId,
            messages: this.messagesList
        };

        this.messagesList = [];
        return list;
    }

    this.addMessage = function(msg) {
        this.messagesList.push(msg);
    }

    this.sendRestMessages = function () {
        this.sendMessages();
    }

    this.sendMessages = function () {
        if (window.isPreviewGame == true) {
            return;
        }
        messageList = this.popMessageList();
        //console.log(JSON.stringify(messageList));
       
        $.ajax({
            type: "POST",
            url: "../MGGameService.svc/recordData/",
            data: JSON.stringify(messageList),
            dataType: 'json'

        });
    }
}